package com.js.purs.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.js.basic.api.RemoteBasicService;
import com.js.basic.api.model.CodeRules;
import com.js.purs.service.IPurProdBudgetService;
import com.js.purs.service.IPurProdInfoService;
import com.js.purs.service.IPurProdPlanFileService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.CreateCode;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.js.purs.domain.PurProdPlan;
import com.js.purs.service.IPurProdPlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购计划.审批通过后生成招标项目Controller
 *
 * @author lzq
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/purs")
public class PurProdPlanController extends BaseController {
    @Autowired
    private IPurProdPlanService purProdPlanService;


    @Autowired
    private RemoteBasicService remoteBasicService;

    @Resource
    private IPurProdInfoService purProdInfoService;

    @Resource
    private IPurProdPlanFileService purProdPlanFileService;

    @Resource
    private IPurProdBudgetService purProdBudgetService;


    /**
     * 查询采购计划.审批通过后生成招标项目列表
     */
    @RequiresPermissions("purs:purs:list")
    @GetMapping("/list")
    public TableDataInfo list(PurProdPlan purProdPlan) {
        startPage();
        List<PurProdPlan> list = purProdPlanService.selectPurProdPlanList(purProdPlan);
        return getDataTable(list);
    }

    /**
     * 导出采购计划.审批通过后生成招标项目列表
     */
    @RequiresPermissions("purs:purs:export")
    @Log(title = "采购计划.审批通过后生成招标项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurProdPlan purProdPlan) {
        List<PurProdPlan> list = purProdPlanService.selectPurProdPlanList(purProdPlan);
        ExcelUtil<PurProdPlan> util = new ExcelUtil<PurProdPlan>(PurProdPlan.class);
        util.exportExcel(response, list, "采购计划.审批通过后生成招标项目数据");
    }

    /**
     * 获取采购计划.审批通过后生成招标项目详细信息
     */
    @RequiresPermissions("purs:purs:query")
    @GetMapping(value = "/{planCode}")
    public AjaxResult getInfo(@PathVariable("planCode") String planCode) {
        return success(purProdPlanService.selectPurProdPlanByPlanCode(planCode));
    }

    /**
     * 新增采购计划.审批通过后生成招标项目
     */
    @RequiresPermissions("purs:purs:add")
    @Log(title = "采购计划.审批通过后生成招标项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurProdPlan purProdPlan) {
        return toAjax(purProdPlanService.insertPurProdPlan(purProdPlan));
    }

    /**
     * 修改采购计划.审批通过后生成招标项目
     */
    @RequiresPermissions("purs:purs:edit")
    @Log(title = "采购计划.审批通过后生成招标项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurProdPlan purProdPlan) {
        return toAjax(purProdPlanService.updatePurProdPlan(purProdPlan));
    }

    /**
     * 删除采购计划.审批通过后生成招标项目
     */
    @RequiresPermissions("purs:purs:remove")
    @Log(title = "采购计划.审批通过后生成招标项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planCodes}")
    public AjaxResult remove(@PathVariable String[] planCodes) {
        return toAjax(purProdPlanService.deletePurProdPlanByPlanCodes(planCodes));
    }

    /**
     * 获取采购计划编号
     */
    @GetMapping("/getCode/{tableName}")
    public AjaxResult getPurId(@PathVariable String tableName) {
        return purProdPlanService.getPlanCode(tableName);
    }


    /**
     * 获取采购计划详情信息（多表查询）
     */

    @GetMapping("/getPlanDetails/{planCode}")
    public AjaxResult getPlanDetails(@PathVariable("planCode") String planCode) {
        PurProdPlan purProdPlan = purProdPlanService.selectPurProdPlanByPlanCode(planCode);
        purProdPlan.setPurProdBudget(purProdBudgetService.selectPurProdBudgetByPlanCode(planCode));
        purProdPlan.setPurProdPlanFiles(purProdPlanFileService.selectPurProdPlanFilePlanCode(planCode));
        return AjaxResult.success(purProdPlan);
    }

    /**
     * 获取未执行采购寻源的计划
     */
    @RequiresPermissions("purs:purs:list")
    @GetMapping("/getPlanFinish")
    public TableDataInfo getPlanFinish() {
        startPage();
        List<PurProdPlan> list = purProdPlanService.selectPurProdPlanByFinish();
        return getDataTable(list);
    }


    //获取最新的四条寻源计划
    @GetMapping("/getPlanFinishNewBest")
    public AjaxResult getPlanFinishNewBest() {

        List<PurProdPlan> list = purProdPlanService.selectPurProdPlanByFinishNewBest();
        return success(list);
    }

    //删除所有与planCode有关的信息
    @DeleteMapping("/clearByPlanCode/{planCode}")
    public AjaxResult clearByPlanCode(@PathVariable("planCode") String planCode) {

        return toAjax(purProdPlanService.delPursByPlanCode(planCode));
    }


}
